<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8.13. enum — Support for enumerations &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="8. Data Types" href="datatypes.html" />
    <link rel="next" title="9. Numeric and Mathematical Modules" href="numeric.html" />
    <link rel="prev" title="8.12. reprlib — Alternate repr() implementation" href="reprlib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="numeric.html" title="9. Numeric and Mathematical Modules"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="reprlib.html" title="8.12. reprlib — Alternate repr() implementation"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" accesskey="U">8. Data Types</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-enum">
<span id="enum-support-for-enumerations"></span><h1>8.13. <a class="reference internal" href="#module-enum" title="enum: Implementation of an enumeration class."><tt class="xref py py-mod docutils literal"><span class="pre">enum</span></tt></a> &#8212; Support for enumerations<a class="headerlink" href="#module-enum" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.4/Lib/enum.py">Lib/enum.py</a></p>
<hr class="docutils" />
<p>An enumeration is a set of symbolic names (members) bound to unique,
constant values.  Within an enumeration, the members can be compared
by identity, and the enumeration itself can be iterated over.</p>
<div class="section" id="module-contents">
<h2>8.13.1. Module Contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2>
<p>This module defines two enumeration classes that can be used to define unique
sets of names and values: <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> and <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a>.  It also defines
one decorator, <a class="reference internal" href="#enum.unique" title="enum.unique"><tt class="xref py py-func docutils literal"><span class="pre">unique()</span></tt></a>.</p>
<dl class="class">
<dt id="enum.Enum">
<em class="property">class </em><tt class="descclassname">enum.</tt><tt class="descname">Enum</tt><a class="headerlink" href="#enum.Enum" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for creating enumerated constants.  See section
<a class="reference internal" href="#functional-api">Functional API</a> for an alternate construction syntax.</p>
</dd></dl>

<dl class="class">
<dt id="enum.IntEnum">
<em class="property">class </em><tt class="descclassname">enum.</tt><tt class="descname">IntEnum</tt><a class="headerlink" href="#enum.IntEnum" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for creating enumerated constants that are also
subclasses of <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="enum.unique">
<tt class="descclassname">enum.</tt><tt class="descname">unique</tt><big>(</big><big>)</big><a class="headerlink" href="#enum.unique" title="Permalink to this definition">¶</a></dt>
<dd><p>Enum class decorator that ensures only one name is bound to any one value.</p>
</dd></dl>

</div>
<div class="section" id="creating-an-enum">
<h2>8.13.2. Creating an Enum<a class="headerlink" href="#creating-an-enum" title="Permalink to this headline">¶</a></h2>
<p>Enumerations are created using the <a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> syntax, which makes them
easy to read and write.  An alternative creation method is described in
<a class="reference internal" href="#functional-api">Functional API</a>.  To define an enumeration, subclass <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> as
follows:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">enum</span> <span class="k">import</span> <span class="n">Enum</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Color</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">red</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">green</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">blue</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">...</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Nomenclature</p>
<ul class="last simple">
<li>The class <tt class="xref py py-class docutils literal"><span class="pre">Color</span></tt> is an <em>enumeration</em> (or <em>enum</em>)</li>
<li>The attributes <tt class="xref py py-attr docutils literal"><span class="pre">Color.red</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">Color.green</span></tt>, etc., are
<em>enumeration members</em> (or <em>enum members</em>).</li>
<li>The enum members have <em>names</em> and <em>values</em> (the name of
<tt class="xref py py-attr docutils literal"><span class="pre">Color.red</span></tt> is <tt class="docutils literal"><span class="pre">red</span></tt>, the value of <tt class="xref py py-attr docutils literal"><span class="pre">Color.blue</span></tt> is
<tt class="docutils literal"><span class="pre">3</span></tt>, etc.)</li>
</ul>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Even though we use the <a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> syntax to create Enums, Enums
are not normal Python classes.  See <a class="reference internal" href="#how-are-enums-different">How are Enums different?</a> for
more details.</p>
</div>
<p>Enumeration members have human readable string representations:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="p">)</span>
<span class="go">Color.red</span>
</pre></div>
</div>
<p>...while their <tt class="docutils literal"><span class="pre">repr</span></tt> has more information:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="p">))</span>
<span class="go">&lt;Color.red: 1&gt;</span>
</pre></div>
</div>
<p>The <em>type</em> of an enumeration member is the enumeration it belongs to:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="p">)</span>
<span class="go">&lt;enum &#39;Color&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">green</span><span class="p">,</span> <span class="n">Color</span><span class="p">)</span>
<span class="go">True</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>Enum members also have a property that contains just their item name:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">red</span>
</pre></div>
</div>
<p>Enumerations support iteration, in definition order:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shake</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">vanilla</span> <span class="o">=</span> <span class="mi">7</span>
<span class="gp">... </span>    <span class="n">chocolate</span> <span class="o">=</span> <span class="mi">4</span>
<span class="gp">... </span>    <span class="n">cookies</span> <span class="o">=</span> <span class="mi">9</span>
<span class="gp">... </span>    <span class="n">mint</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">shake</span> <span class="ow">in</span> <span class="n">Shake</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">shake</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">Shake.vanilla</span>
<span class="go">Shake.chocolate</span>
<span class="go">Shake.cookies</span>
<span class="go">Shake.mint</span>
</pre></div>
</div>
<p>Enumeration members are hashable, so they can be used in dictionaries and sets:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">apples</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">apples</span><span class="p">[</span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;red delicious&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">apples</span><span class="p">[</span><span class="n">Color</span><span class="o">.</span><span class="n">green</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;granny smith&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">apples</span> <span class="o">==</span> <span class="p">{</span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="p">:</span> <span class="s">&#39;red delicious&#39;</span><span class="p">,</span> <span class="n">Color</span><span class="o">.</span><span class="n">green</span><span class="p">:</span> <span class="s">&#39;granny smith&#39;</span><span class="p">}</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="programmatic-access-to-enumeration-members-and-their-attributes">
<h2>8.13.3. Programmatic access to enumeration members and their attributes<a class="headerlink" href="#programmatic-access-to-enumeration-members-and-their-attributes" title="Permalink to this headline">¶</a></h2>
<p>Sometimes it&#8217;s useful to access members in enumerations programmatically (i.e.
situations where <tt class="docutils literal"><span class="pre">Color.red</span></tt> won&#8217;t do because the exact color is not known
at program-writing time).  <tt class="docutils literal"><span class="pre">Enum</span></tt> allows such access:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">&lt;Color.red: 1&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">&lt;Color.blue: 3&gt;</span>
</pre></div>
</div>
<p>If you want to access enum members by <em>name</em>, use item access:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="p">[</span><span class="s">&#39;red&#39;</span><span class="p">]</span>
<span class="go">&lt;Color.red: 1&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="p">[</span><span class="s">&#39;green&#39;</span><span class="p">]</span>
<span class="go">&lt;Color.green: 2&gt;</span>
</pre></div>
</div>
<p>If you have an enum member and need its <tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt> or <tt class="xref py py-attr docutils literal"><span class="pre">value</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">member</span> <span class="o">=</span> <span class="n">Color</span><span class="o">.</span><span class="n">red</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">member</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;red&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">member</span><span class="o">.</span><span class="n">value</span>
<span class="go">1</span>
</pre></div>
</div>
</div>
<div class="section" id="duplicating-enum-members-and-values">
<h2>8.13.4. Duplicating enum members and values<a class="headerlink" href="#duplicating-enum-members-and-values" title="Permalink to this headline">¶</a></h2>
<p>Having two enum members with the same name is invalid:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">square</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">square</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">Attempted to reuse key: &#39;square&#39;</span>
</pre></div>
</div>
<p>However, two enum members are allowed to have the same value.  Given two members
A and B with the same value (and A defined first), B is an alias to A.  By-value
lookup of the value of A and B will return A.  By-name lookup of B will also
return A:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">square</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">diamond</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">circle</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">... </span>    <span class="n">alias_for_square</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span><span class="o">.</span><span class="n">square</span>
<span class="go">&lt;Shape.square: 2&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span><span class="o">.</span><span class="n">alias_for_square</span>
<span class="go">&lt;Shape.square: 2&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">&lt;Shape.square: 2&gt;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Attempting to create a member with the same name as an already
defined attribute (another member, a method, etc.) or attempting to create
an attribute with the same name as a member is not allowed.</p>
</div>
</div>
<div class="section" id="ensuring-unique-enumeration-values">
<h2>8.13.5. Ensuring unique enumeration values<a class="headerlink" href="#ensuring-unique-enumeration-values" title="Permalink to this headline">¶</a></h2>
<p>By default, enumerations allow multiple names as aliases for the same value.
When this behavior isn&#8217;t desired, the following decorator can be used to
ensure each value is used only once in the enumeration:</p>
<dl class="function">
<dt>
<tt class="descclassname">&#64;</tt><tt class="descclassname">enum.</tt><tt class="descname">unique</tt></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> decorator specifically for enumerations.  It searches an
enumeration&#8217;s <tt class="xref py py-attr docutils literal"><span class="pre">__members__</span></tt> gathering any aliases it finds; if any are
found <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised with the details:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">enum</span> <span class="k">import</span> <span class="n">Enum</span><span class="p">,</span> <span class="n">unique</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@unique</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Mistake</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">one</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">two</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">three</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">... </span>    <span class="n">four</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">duplicate values found in &lt;enum &#39;Mistake&#39;&gt;: four -&gt; three</span>
</pre></div>
</div>
</div>
<div class="section" id="iteration">
<h2>8.13.6. Iteration<a class="headerlink" href="#iteration" title="Permalink to this headline">¶</a></h2>
<p>Iterating over the members of an enum does not provide the aliases:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">Shape</span><span class="p">)</span>
<span class="go">[&lt;Shape.square: 2&gt;, &lt;Shape.diamond: 1&gt;, &lt;Shape.circle: 3&gt;]</span>
</pre></div>
</div>
<p>The special attribute <tt class="docutils literal"><span class="pre">__members__</span></tt> is an ordered dictionary mapping names
to members.  It includes all names defined in the enumeration, including the
aliases:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">member</span> <span class="ow">in</span> <span class="n">Shape</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="gp">... </span>    <span class="n">name</span><span class="p">,</span> <span class="n">member</span>
<span class="gp">...</span>
<span class="go">(&#39;square&#39;, &lt;Shape.square: 2&gt;)</span>
<span class="go">(&#39;diamond&#39;, &lt;Shape.diamond: 1&gt;)</span>
<span class="go">(&#39;circle&#39;, &lt;Shape.circle: 3&gt;)</span>
<span class="go">(&#39;alias_for_square&#39;, &lt;Shape.square: 2&gt;)</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">__members__</span></tt> attribute can be used for detailed programmatic access to
the enumeration members.  For example, finding all the aliases:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">name</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">member</span> <span class="ow">in</span> <span class="n">Shape</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">member</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="n">name</span><span class="p">]</span>
<span class="go">[&#39;alias_for_square&#39;]</span>
</pre></div>
</div>
</div>
<div class="section" id="comparisons">
<h2>8.13.7. Comparisons<a class="headerlink" href="#comparisons" title="Permalink to this headline">¶</a></h2>
<p>Enumeration members are compared by identity:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">red</span> <span class="ow">is</span> <span class="n">Color</span><span class="o">.</span><span class="n">red</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">red</span> <span class="ow">is</span> <span class="n">Color</span><span class="o">.</span><span class="n">blue</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">red</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Color</span><span class="o">.</span><span class="n">blue</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Ordered comparisons between enumeration values are <em>not</em> supported.  Enum
members are not integers (but see <a class="reference internal" href="#intenum">IntEnum</a> below):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">red</span> <span class="o">&lt;</span> <span class="n">Color</span><span class="o">.</span><span class="n">blue</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">unorderable types: Color() &lt; Color()</span>
</pre></div>
</div>
<p>Equality comparisons are defined though:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">blue</span> <span class="o">==</span> <span class="n">Color</span><span class="o">.</span><span class="n">red</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">blue</span> <span class="o">!=</span> <span class="n">Color</span><span class="o">.</span><span class="n">red</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">blue</span> <span class="o">==</span> <span class="n">Color</span><span class="o">.</span><span class="n">blue</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Comparisons against non-enumeration values will always compare not equal
(again, <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> was explicitly designed to behave differently, see
below):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">blue</span> <span class="o">==</span> <span class="mi">2</span>
<span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="allowed-members-and-attributes-of-enumerations">
<h2>8.13.8. Allowed members and attributes of enumerations<a class="headerlink" href="#allowed-members-and-attributes-of-enumerations" title="Permalink to this headline">¶</a></h2>
<p>The examples above use integers for enumeration values.  Using integers is
short and handy (and provided by default by the <a class="reference internal" href="#functional-api">Functional API</a>), but not
strictly enforced.  In the vast majority of use-cases, one doesn&#8217;t care what
the actual value of an enumeration is.  But if the value <em>is</em> important,
enumerations can have arbitrary values.</p>
<p>Enumerations are Python classes, and can have methods and special methods as
usual.  If we have this enumeration:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Mood</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">funky</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">happy</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c"># self is the member here</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="s">&#39;my custom str! {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="nd">@classmethod</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">favorite_mood</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c"># cls here is the enumeration</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">happy</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>Then:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Mood</span><span class="o">.</span><span class="n">favorite_mood</span><span class="p">()</span>
<span class="go">&lt;Mood.happy: 3&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Mood</span><span class="o">.</span><span class="n">happy</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="go">(&#39;happy&#39;, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">Mood</span><span class="o">.</span><span class="n">funky</span><span class="p">)</span>
<span class="go">&#39;my custom str! 1&#39;</span>
</pre></div>
</div>
<p>The rules for what is allowed are as follows: _sunder_ names (starting and
ending with a single underscore) are reserved by enum and cannot be used;
all other attributes defined within an enumeration will become members of this
enumeration, with the exception of <em>__dunder__</em> names and descriptors (methods
are also descriptors).</p>
<p>Note:  if your enumeration defines <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> and/or <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> then
whatever value(s) were given to the enum member will be passed into those
methods.  See <a class="reference internal" href="#planet">Planet</a> for an example.</p>
</div>
<div class="section" id="restricted-subclassing-of-enumerations">
<h2>8.13.9. Restricted subclassing of enumerations<a class="headerlink" href="#restricted-subclassing-of-enumerations" title="Permalink to this headline">¶</a></h2>
<p>Subclassing an enumeration is allowed only if the enumeration does not define
any members.  So this is forbidden:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MoreColor</span><span class="p">(</span><span class="n">Color</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">pink</span> <span class="o">=</span> <span class="mi">17</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">Cannot extend enumerations</span>
</pre></div>
</div>
<p>But this is allowed:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">some_behavior</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Bar</span><span class="p">(</span><span class="n">Foo</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">happy</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">sad</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>Allowing subclassing of enums that define members would lead to a violation of
some important invariants of types and instances.  On the other hand, it makes
sense to allow sharing some common behavior between a group of enumerations.
(See <a class="reference internal" href="#orderedenum">OrderedEnum</a> for an example.)</p>
</div>
<div class="section" id="pickling">
<h2>8.13.10. Pickling<a class="headerlink" href="#pickling" title="Permalink to this headline">¶</a></h2>
<p>Enumerations can be pickled and unpickled:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">test.test_enum</span> <span class="k">import</span> <span class="n">Fruit</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pickle</span> <span class="k">import</span> <span class="n">dumps</span><span class="p">,</span> <span class="n">loads</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Fruit</span><span class="o">.</span><span class="n">tomato</span> <span class="ow">is</span> <span class="n">loads</span><span class="p">(</span><span class="n">dumps</span><span class="p">(</span><span class="n">Fruit</span><span class="o">.</span><span class="n">tomato</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The usual restrictions for pickling apply: picklable enums must be defined in
the top level of a module, since unpickling requires them to be importable
from that module.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">With pickle protocol version 4 it is possible to easily pickle enums
nested in other classes.</p>
</div>
<p>It is possible to modify how Enum members are pickled/unpickled by defining
<a class="reference internal" href="pickle.html#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> in the enumeration class.</p>
</div>
<div class="section" id="functional-api">
<h2>8.13.11. Functional API<a class="headerlink" href="#functional-api" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> class is callable, providing the following functional API:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span> <span class="o">=</span> <span class="n">Enum</span><span class="p">(</span><span class="s">&#39;Animal&#39;</span><span class="p">,</span> <span class="s">&#39;ant bee cat dog&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span>
<span class="go">&lt;enum &#39;Animal&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span><span class="o">.</span><span class="n">ant</span>
<span class="go">&lt;Animal.ant: 1&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span><span class="o">.</span><span class="n">ant</span><span class="o">.</span><span class="n">value</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">Animal</span><span class="p">)</span>
<span class="go">[&lt;Animal.ant: 1&gt;, &lt;Animal.bee: 2&gt;, &lt;Animal.cat: 3&gt;, &lt;Animal.dog: 4&gt;]</span>
</pre></div>
</div>
<p>The semantics of this API resemble <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-class docutils literal"><span class="pre">namedtuple</span></tt></a>. The first
argument of the call to <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> is the name of the enumeration.</p>
<p>The second argument is the <em>source</em> of enumeration member names.  It can be a
whitespace-separated string of names, a sequence of names, a sequence of
2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
values.  The last two options enable assigning arbitrary values to
enumerations; the others auto-assign increasing integers starting with 1.  A
new class derived from <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> is returned.  In other words, the above
assignment to <tt class="xref py py-class docutils literal"><span class="pre">Animal</span></tt> is equivalent to:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Animal</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">ant</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">bee</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">cat</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">... </span>    <span class="n">dog</span> <span class="o">=</span> <span class="mi">4</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>The reason for defaulting to <tt class="docutils literal"><span class="pre">1</span></tt> as the starting number and not <tt class="docutils literal"><span class="pre">0</span></tt> is
that <tt class="docutils literal"><span class="pre">0</span></tt> is <tt class="docutils literal"><span class="pre">False</span></tt> in a boolean sense, but enum members all evaluate
to <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>Pickling enums created with the functional API can be tricky as frame stack
implementation details are used to try and figure out which module the
enumeration is being created in (e.g. it will fail if you use a utility
function in separate module, and also may not work on IronPython or Jython).
The solution is to specify the module name explicitly as follows:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span> <span class="o">=</span> <span class="n">Enum</span><span class="p">(</span><span class="s">&#39;Animal&#39;</span><span class="p">,</span> <span class="s">&#39;ant bee cat dog&#39;</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="n">__name__</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If <tt class="docutils literal"><span class="pre">module</span></tt> is not supplied, and Enum cannot determine what it is,
the new Enum members will not be unpicklable; to keep errors closer to
the source, pickling will be disabled.</p>
</div>
<p>The new pickle protocol 4 also, in some circumstances, relies on
<tt class="xref py py-attr docutils literal"><span class="pre">__qualname__</span></tt> being set to the location where pickle will be able
to find the class.  For example, if the class was made available in class
SomeData in the global scope:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span> <span class="o">=</span> <span class="n">Enum</span><span class="p">(</span><span class="s">&#39;Animal&#39;</span><span class="p">,</span> <span class="s">&#39;ant bee cat dog&#39;</span><span class="p">,</span> <span class="n">qualname</span><span class="o">=</span><span class="s">&#39;SomeData.Animal&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The complete signature is:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Enum</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="s">&#39;NewEnumName&#39;</span><span class="p">,</span> <span class="n">names</span><span class="o">=&lt;...&gt;</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s">&#39;...&#39;</span><span class="p">,</span> <span class="n">qualname</span><span class="o">=</span><span class="s">&#39;...&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=&lt;</span><span class="n">mixed</span><span class="o">-</span><span class="ow">in</span> <span class="n">class</span><span class="o">&gt;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">value:</th><td class="field-body"><p class="first">What the new Enum class will record as its name.</p>
</td>
</tr>
<tr class="field-even field"><th class="field-name">names:</th><td class="field-body"><p class="first">The Enum members.  This can be a whitespace or comma separated string
(values will start at 1):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="s">&#39;red green blue&#39;</span> <span class="o">|</span> <span class="s">&#39;red,green,blue&#39;</span> <span class="o">|</span> <span class="s">&#39;red, green, blue&#39;</span>
</pre></div>
</div>
<p>or an iterator of names:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">[</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="s">&#39;green&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>or an iterator of (name, value) pairs:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">[(</span><span class="s">&#39;cyan&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;magenta&#39;</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;yellow&#39;</span><span class="p">,</span> <span class="mi">6</span><span class="p">)]</span>
</pre></div>
</div>
<p>or a mapping:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">{</span><span class="s">&#39;chartreuse&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s">&#39;sea_green&#39;</span><span class="p">:</span> <span class="mi">11</span><span class="p">,</span> <span class="s">&#39;rosemary&#39;</span><span class="p">:</span> <span class="mi">42</span><span class="p">}</span>
</pre></div>
</div>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">module:</th><td class="field-body"><p class="first">name of module where new Enum class can be found.</p>
</td>
</tr>
<tr class="field-even field"><th class="field-name">qualname:</th><td class="field-body"><p class="first">where in module new Enum class can be found.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">type:</th><td class="field-body"><p class="first last">type to mix in to new Enum class.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="derived-enumerations">
<h2>8.13.12. Derived Enumerations<a class="headerlink" href="#derived-enumerations" title="Permalink to this headline">¶</a></h2>
<div class="section" id="intenum">
<h3>8.13.12.1. IntEnum<a class="headerlink" href="#intenum" title="Permalink to this headline">¶</a></h3>
<p>A variation of <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> is provided which is also a subclass of
<a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>.  Members of an <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> can be compared to integers;
by extension, integer enumerations of different types can also be compared
to each other:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">enum</span> <span class="k">import</span> <span class="n">IntEnum</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="n">IntEnum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">circle</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">square</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Request</span><span class="p">(</span><span class="n">IntEnum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">post</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">get</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span> <span class="o">==</span> <span class="mi">1</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span><span class="o">.</span><span class="n">circle</span> <span class="o">==</span> <span class="mi">1</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span><span class="o">.</span><span class="n">circle</span> <span class="o">==</span> <span class="n">Request</span><span class="o">.</span><span class="n">post</span>
<span class="go">True</span>
</pre></div>
</div>
<p>However, they still can&#8217;t be compared to standard <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> enumerations:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="n">IntEnum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">circle</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">square</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Color</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">red</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">green</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Shape</span><span class="o">.</span><span class="n">circle</span> <span class="o">==</span> <span class="n">Color</span><span class="o">.</span><span class="n">red</span>
<span class="go">False</span>
</pre></div>
</div>
<p><a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> values behave like integers in other ways you&#8217;d expect:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="n">Shape</span><span class="o">.</span><span class="n">circle</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">][</span><span class="n">Shape</span><span class="o">.</span><span class="n">circle</span><span class="p">]</span>
<span class="go">&#39;b&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">Shape</span><span class="o">.</span><span class="n">square</span><span class="p">)]</span>
<span class="go">[0, 1]</span>
</pre></div>
</div>
<p>For the vast majority of code, <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> is strongly recommended,
since <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> breaks some semantic promises of an enumeration (by
being comparable to integers, and thus by transitivity to other
unrelated enumerations).  It should be used only in special cases where
there&#8217;s no other choice; for example, when integer constants are
replaced with enumerations and backwards compatibility is required with code
that still expects integers.</p>
</div>
<div class="section" id="others">
<h3>8.13.12.2. Others<a class="headerlink" href="#others" title="Permalink to this headline">¶</a></h3>
<p>While <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> is part of the <a class="reference internal" href="#module-enum" title="enum: Implementation of an enumeration class."><tt class="xref py py-mod docutils literal"><span class="pre">enum</span></tt></a> module, it would be very
simple to implement independently:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">IntEnum</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">Enum</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>This demonstrates how similar derived enumerations can be defined; for example
a <tt class="xref py py-class docutils literal"><span class="pre">StrEnum</span></tt> that mixes in <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> instead of <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>.</p>
<p>Some rules:</p>
<ol class="arabic simple">
<li>When subclassing <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a>, mix-in types must appear before
<a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> itself in the sequence of bases, as in the <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a>
example above.</li>
<li>While <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> can have members of any type, once you mix in an
additional type, all the members must have values of that type, e.g.
<a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> above.  This restriction does not apply to mix-ins which only
add methods and don&#8217;t specify another data type such as <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> or
<a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>.</li>
<li>When another data type is mixed in, the <tt class="xref py py-attr docutils literal"><span class="pre">value</span></tt> attribute is <em>not the
same</em> as the enum member itself, although it is equivalent and will compare
equal.</li>
<li>%-style formatting:  <cite>%s</cite> and <cite>%r</cite> call <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a>&#8216;s <a class="reference internal" href="../reference/datamodel.html#object.__str__" title="object.__str__"><tt class="xref py py-meth docutils literal"><span class="pre">__str__()</span></tt></a> and
<a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> respectively; other codes (such as <cite>%i</cite> or <cite>%h</cite> for
IntEnum) treat the enum member as its mixed-in type.</li>
<li><tt class="xref py py-meth docutils literal"><span class="pre">str.__format__()</span></tt> (or <a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a>) will use the mixed-in
type&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt></a>.  If the <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a>&#8216;s <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> or
<a class="reference internal" href="functions.html#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> is desired use the <cite>!s</cite> or <cite>!r</cite> <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> format codes.</li>
</ol>
</div>
</div>
<div class="section" id="interesting-examples">
<h2>8.13.13. Interesting examples<a class="headerlink" href="#interesting-examples" title="Permalink to this headline">¶</a></h2>
<p>While <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> and <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> are expected to cover the majority of
use-cases, they cannot cover them all.  Here are recipes for some different
types of enumerations that can be used directly, or as examples for creating
one&#8217;s own.</p>
<div class="section" id="autonumber">
<h3>8.13.13.1. AutoNumber<a class="headerlink" href="#autonumber" title="Permalink to this headline">¶</a></h3>
<p>Avoids having to specify the value for each enumeration member:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">AutoNumber</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">value</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">cls</span><span class="o">.</span><span class="n">__members__</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">... </span>        <span class="n">obj</span> <span class="o">=</span> <span class="nb">object</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">)</span>
<span class="gp">... </span>        <span class="n">obj</span><span class="o">.</span><span class="n">_value_</span> <span class="o">=</span> <span class="n">value</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">obj</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Color</span><span class="p">(</span><span class="n">AutoNumber</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">red</span> <span class="o">=</span> <span class="p">()</span>
<span class="gp">... </span>    <span class="n">green</span> <span class="o">=</span> <span class="p">()</span>
<span class="gp">... </span>    <span class="n">blue</span> <span class="o">=</span> <span class="p">()</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">green</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="mi">2</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> method, if defined, is used during creation of the Enum
members; it is then replaced by Enum&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> which is used after
class creation for lookup of existing members.</p>
</div>
</div>
<div class="section" id="orderedenum">
<h3>8.13.13.2. OrderedEnum<a class="headerlink" href="#orderedenum" title="Permalink to this headline">¶</a></h3>
<p>An ordered enumeration that is not based on <a class="reference internal" href="#enum.IntEnum" title="enum.IntEnum"><tt class="xref py py-class docutils literal"><span class="pre">IntEnum</span></tt></a> and so maintains
the normal <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> invariants (such as not being comparable to other
enumerations):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">OrderedEnum</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__ge__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="n">other</span><span class="o">.</span><span class="n">__class__</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&gt;=</span> <span class="n">other</span><span class="o">.</span><span class="n">value</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">NotImplemented</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__gt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="n">other</span><span class="o">.</span><span class="n">__class__</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&gt;</span> <span class="n">other</span><span class="o">.</span><span class="n">value</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">NotImplemented</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__le__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="n">other</span><span class="o">.</span><span class="n">__class__</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;=</span> <span class="n">other</span><span class="o">.</span><span class="n">value</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">NotImplemented</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="n">other</span><span class="o">.</span><span class="n">__class__</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">value</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">NotImplemented</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Grade</span><span class="p">(</span><span class="n">OrderedEnum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">A</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">... </span>    <span class="n">B</span> <span class="o">=</span> <span class="mi">4</span>
<span class="gp">... </span>    <span class="n">C</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">... </span>    <span class="n">D</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">F</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Grade</span><span class="o">.</span><span class="n">C</span> <span class="o">&lt;</span> <span class="n">Grade</span><span class="o">.</span><span class="n">A</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="duplicatefreeenum">
<h3>8.13.13.3. DuplicateFreeEnum<a class="headerlink" href="#duplicatefreeenum" title="Permalink to this headline">¶</a></h3>
<p>Raises an error if a duplicate member name is found instead of creating an
alias:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">DuplicateFreeEnum</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">cls</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">cls</span><span class="p">):</span>
<span class="gp">... </span>            <span class="n">a</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
<span class="gp">... </span>            <span class="n">e</span> <span class="o">=</span> <span class="n">cls</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">name</span>
<span class="gp">... </span>            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
<span class="gp">... </span>                <span class="s">&quot;aliases not allowed in DuplicateFreeEnum:  %r --&gt; %r&quot;</span>
<span class="gp">... </span>                <span class="o">%</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Color</span><span class="p">(</span><span class="n">DuplicateFreeEnum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">red</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">green</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span>    <span class="n">blue</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">... </span>    <span class="n">grene</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">aliases not allowed in DuplicateFreeEnum:  &#39;grene&#39; --&gt; &#39;green&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This is a useful example for subclassing Enum to add or change other
behaviors as well as disallowing aliases.  If the only desired change is
disallowing aliases, the <a class="reference internal" href="#enum.unique" title="enum.unique"><tt class="xref py py-func docutils literal"><span class="pre">unique()</span></tt></a> decorator can be used instead.</p>
</div>
</div>
<div class="section" id="planet">
<h3>8.13.13.4. Planet<a class="headerlink" href="#planet" title="Permalink to this headline">¶</a></h3>
<p>If <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> is defined the value of the enum member
will be passed to those methods:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Planet</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">MERCURY</span> <span class="o">=</span> <span class="p">(</span><span class="mf">3.303e+23</span><span class="p">,</span> <span class="mf">2.4397e6</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">VENUS</span>   <span class="o">=</span> <span class="p">(</span><span class="mf">4.869e+24</span><span class="p">,</span> <span class="mf">6.0518e6</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">EARTH</span>   <span class="o">=</span> <span class="p">(</span><span class="mf">5.976e+24</span><span class="p">,</span> <span class="mf">6.37814e6</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">MARS</span>    <span class="o">=</span> <span class="p">(</span><span class="mf">6.421e+23</span><span class="p">,</span> <span class="mf">3.3972e6</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">JUPITER</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.9e+27</span><span class="p">,</span>   <span class="mf">7.1492e7</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">SATURN</span>  <span class="o">=</span> <span class="p">(</span><span class="mf">5.688e+26</span><span class="p">,</span> <span class="mf">6.0268e7</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">URANUS</span>  <span class="o">=</span> <span class="p">(</span><span class="mf">8.686e+25</span><span class="p">,</span> <span class="mf">2.5559e7</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">NEPTUNE</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.024e+26</span><span class="p">,</span> <span class="mf">2.4746e7</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mass</span><span class="p">,</span> <span class="n">radius</span><span class="p">):</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">mass</span> <span class="o">=</span> <span class="n">mass</span>       <span class="c"># in kilograms</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">radius</span> <span class="o">=</span> <span class="n">radius</span>   <span class="c"># in meters</span>
<span class="gp">... </span>    <span class="nd">@property</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">surface_gravity</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c"># universal gravitational constant  (m3 kg-1 s-2)</span>
<span class="gp">... </span>        <span class="n">G</span> <span class="o">=</span> <span class="mf">6.67300E-11</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">G</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">mass</span> <span class="o">/</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">radius</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">radius</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Planet</span><span class="o">.</span><span class="n">EARTH</span><span class="o">.</span><span class="n">value</span>
<span class="go">(5.976e+24, 6378140.0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Planet</span><span class="o">.</span><span class="n">EARTH</span><span class="o">.</span><span class="n">surface_gravity</span>
<span class="go">9.802652743337129</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="how-are-enums-different">
<h2>8.13.14. How are Enums different?<a class="headerlink" href="#how-are-enums-different" title="Permalink to this headline">¶</a></h2>
<p>Enums have a custom metaclass that affects many aspects of both derived Enum
classes and their instances (members).</p>
<div class="section" id="enum-classes">
<h3>8.13.14.1. Enum Classes<a class="headerlink" href="#enum-classes" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="xref py py-class docutils literal"><span class="pre">EnumMeta</span></tt> metaclass is responsible for providing the
<a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__dir__" title="object.__dir__"><tt class="xref py py-meth docutils literal"><span class="pre">__dir__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> and other methods that
allow one to do things with an <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> class that fail on a typical
class, such as <cite>list(Color)</cite> or <cite>some_var in Color</cite>.  <tt class="xref py py-class docutils literal"><span class="pre">EnumMeta</span></tt> is
responsible for ensuring that various other methods on the final <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a>
class are correct (such as <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a>, <a class="reference internal" href="pickle.html#object.__getnewargs__" title="object.__getnewargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getnewargs__()</span></tt></a>,
<a class="reference internal" href="../reference/datamodel.html#object.__str__" title="object.__str__"><tt class="xref py py-meth docutils literal"><span class="pre">__str__()</span></tt></a> and <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a>)</p>
</div>
<div class="section" id="enum-members-aka-instances">
<h3>8.13.14.2. Enum Members (aka instances)<a class="headerlink" href="#enum-members-aka-instances" title="Permalink to this headline">¶</a></h3>
<p>The most interesting thing about Enum members is that they are singletons.
<tt class="xref py py-class docutils literal"><span class="pre">EnumMeta</span></tt> creates them all while it is creating the <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a>
class itself, and then puts a custom <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> in place to ensure
that no new ones are ever instantiated by returning only the existing
member instances.</p>
</div>
<div class="section" id="finer-points">
<h3>8.13.14.3. Finer Points<a class="headerlink" href="#finer-points" title="Permalink to this headline">¶</a></h3>
<p>Enum members are instances of an Enum class, and even though they are
accessible as <cite>EnumClass.member</cite>, they are not accessible directly from
the member:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">red</span>
<span class="go">&lt;Color.red: 1&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span><span class="o">.</span><span class="n">red</span><span class="o">.</span><span class="n">blue</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AttributeError</span>: <span class="n">&#39;Color&#39; object has no attribute &#39;blue&#39;</span>
</pre></div>
</div>
<p>Likewise, the <tt class="xref py py-attr docutils literal"><span class="pre">__members__</span></tt> is only available on the class.</p>
<p>If you give your <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> subclass extra methods, like the <a class="reference internal" href="#planet">Planet</a>
class above, those methods will show up in a <a class="reference internal" href="functions.html#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a> of the member,
but not of the class:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">Planet</span><span class="p">)</span>
<span class="go">[&#39;EARTH&#39;, &#39;JUPITER&#39;, &#39;MARS&#39;, &#39;MERCURY&#39;, &#39;NEPTUNE&#39;, &#39;SATURN&#39;, &#39;URANUS&#39;, &#39;VENUS&#39;, &#39;__class__&#39;, &#39;__doc__&#39;, &#39;__members__&#39;, &#39;__module__&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">Planet</span><span class="o">.</span><span class="n">EARTH</span><span class="p">)</span>
<span class="go">[&#39;__class__&#39;, &#39;__doc__&#39;, &#39;__module__&#39;, &#39;name&#39;, &#39;surface_gravity&#39;, &#39;value&#39;]</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> method will only be used for the creation of the
<a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> members &#8211; after that it is replaced.  Any custom <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a>
method must create the object and set the <tt class="xref py py-attr docutils literal"><span class="pre">_value_</span></tt> attribute
appropriately.</p>
<p>If you wish to change how <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a> members are looked up you should either
write a helper function or a <a class="reference internal" href="functions.html#classmethod" title="classmethod"><tt class="xref py py-func docutils literal"><span class="pre">classmethod()</span></tt></a> for the <a class="reference internal" href="#enum.Enum" title="enum.Enum"><tt class="xref py py-class docutils literal"><span class="pre">Enum</span></tt></a>
subclass.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">8.13. <tt class="docutils literal"><span class="pre">enum</span></tt> &#8212; Support for enumerations</a><ul>
<li><a class="reference internal" href="#module-contents">8.13.1. Module Contents</a></li>
<li><a class="reference internal" href="#creating-an-enum">8.13.2. Creating an Enum</a></li>
<li><a class="reference internal" href="#programmatic-access-to-enumeration-members-and-their-attributes">8.13.3. Programmatic access to enumeration members and their attributes</a></li>
<li><a class="reference internal" href="#duplicating-enum-members-and-values">8.13.4. Duplicating enum members and values</a></li>
<li><a class="reference internal" href="#ensuring-unique-enumeration-values">8.13.5. Ensuring unique enumeration values</a></li>
<li><a class="reference internal" href="#iteration">8.13.6. Iteration</a></li>
<li><a class="reference internal" href="#comparisons">8.13.7. Comparisons</a></li>
<li><a class="reference internal" href="#allowed-members-and-attributes-of-enumerations">8.13.8. Allowed members and attributes of enumerations</a></li>
<li><a class="reference internal" href="#restricted-subclassing-of-enumerations">8.13.9. Restricted subclassing of enumerations</a></li>
<li><a class="reference internal" href="#pickling">8.13.10. Pickling</a></li>
<li><a class="reference internal" href="#functional-api">8.13.11. Functional API</a></li>
<li><a class="reference internal" href="#derived-enumerations">8.13.12. Derived Enumerations</a><ul>
<li><a class="reference internal" href="#intenum">8.13.12.1. IntEnum</a></li>
<li><a class="reference internal" href="#others">8.13.12.2. Others</a></li>
</ul>
</li>
<li><a class="reference internal" href="#interesting-examples">8.13.13. Interesting examples</a><ul>
<li><a class="reference internal" href="#autonumber">8.13.13.1. AutoNumber</a></li>
<li><a class="reference internal" href="#orderedenum">8.13.13.2. OrderedEnum</a></li>
<li><a class="reference internal" href="#duplicatefreeenum">8.13.13.3. DuplicateFreeEnum</a></li>
<li><a class="reference internal" href="#planet">8.13.13.4. Planet</a></li>
</ul>
</li>
<li><a class="reference internal" href="#how-are-enums-different">8.13.14. How are Enums different?</a><ul>
<li><a class="reference internal" href="#enum-classes">8.13.14.1. Enum Classes</a></li>
<li><a class="reference internal" href="#enum-members-aka-instances">8.13.14.2. Enum Members (aka instances)</a></li>
<li><a class="reference internal" href="#finer-points">8.13.14.3. Finer Points</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="reprlib.html"
                        title="previous chapter">8.12. <tt class="docutils literal"><span class="pre">reprlib</span></tt> &#8212; Alternate <tt class="docutils literal"><span class="pre">repr()</span></tt> implementation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numeric.html"
                        title="next chapter">9. Numeric and Mathematical Modules</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/enum.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="numeric.html" title="9. Numeric and Mathematical Modules"
             >next</a> |</li>
        <li class="right" >
          <a href="reprlib.html" title="8.12. reprlib — Alternate repr() implementation"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" >8. Data Types</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Feb 26, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>